package com.example.xd.permissionlibrary

import android.app.Activity
import android.app.role.RoleManager
import android.content.Intent
import android.net.Uri
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.provider.Settings
import android.telecom.TelecomManager
import com.example.xd.permissionlibrary.utils.isDefaultCall
import com.example.xd.permissionlibrary.utils.isFloatingPermission
import com.example.xd.permissionlibrary.utils.toast

const val FLOATING_CODE = 1
const val DEFAULT_CALL_CODE = 2
const val DEFAULT_CALL_Q_CODE = 3


class SpecialPermissionActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_get_default_call)
        val type = intent.getStringExtra("type")
        when (type) {
            "default_call" -> {
                defaultcall()
            }
            "floating" -> {
                floating()
            }
        }

    }

    private fun floating() {
        var intent = Intent(
            Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
            Uri.parse("package:" + packageName)
        )
        startActivityForResult(intent, FLOATING_CODE)
    }

    private fun defaultcall() {
        if (!isDefaultCall(this)) {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
                toast(this, "版本太低，不支持默认通话")
                return
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                val roleManager = getSystemService(RoleManager::class.java)
                val roleIntent = roleManager.createRequestRoleIntent(RoleManager.ROLE_DIALER)
                startActivityForResult(roleIntent, DEFAULT_CALL_Q_CODE)
                return
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                val intent = Intent(TelecomManager.ACTION_CHANGE_DEFAULT_DIALER)
                intent.putExtra(
                    TelecomManager.EXTRA_CHANGE_DEFAULT_DIALER_PACKAGE_NAME,
                    packageName
                )
                startActivityForResult(intent, DEFAULT_CALL_CODE)
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (requestCode) {
            FLOATING_CODE -> {
                if (isFloatingPermission(this)) {
                    toast(this, "悬浮窗权限获取成功")
                } else {
                    toast(this, "悬浮窗权限获取失败")
                }
                finish()
            }
            DEFAULT_CALL_CODE -> {
                if (resultCode == Activity.RESULT_OK) {
                    toast(this, "默认通话权限获取成功")
                } else {
                    toast(this, "默认通话权限获取失败")
                }
                finish()
            }
            DEFAULT_CALL_Q_CODE -> {
                if (resultCode == Activity.RESULT_OK) {
                    toast(this, "默认通话权限获取成功")
                } else {
                    toast(this, "默认通话权限获取失败")
                }
                finish()

            }
        }


    }
}